import myToast from './toast'

var toastInstance = null

function newInstance() {
  var div = document.createElement('div')
  div.innerHTML = '<my-toast></my-toast>'
  document.body.appendChild(div)

  var my_toast = new Vue({
    el: div,
    components: { myToast }
  }).$children[0]

  return {
    update(options) {
      my_toast.update(options)
    },
    destory() {
      document.body.removeChild(div)
    }
  }
}

function getToastInstance() {
  toastInstance = toastInstance || newInstance()

  return toastInstance
}

function start() {
  getToastInstance()
  toastInstance.update({
    show: true
  })
}

function finish() {
  getToastInstance()
  toastInstance.update({
    show: false
  })
}

function destory() {
  var instance = getToastInstance()
  toastInstance = null
  instance.destory()
}

export default {
  start,
  finish,
  destory
}
